home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-07-03 | 7.5 KB | 117 lines | [TEXT/R*ch] |
- (* Splayset -- modified for Moscow ML 1995-04-22
- * from SML/NJ library v. 0.2
- *
- * COPYRIGHT (c) 1993 by AT&T Bell Laboratories.
- * See file mosml/copyrght/copyrght.att for details.
- *
- * Set of values with an ordering relation, implemented as splay-trees.
- *)
-
- open Splaytree
-
- datatype 'key set =
- OS of {cmpKey : 'key * 'key -> ordering,
- root : 'key splay ref,
- nobj : int}
-
- exception NotFound
- fun cmpf cmpKey k = fn k' => cmpKey(k',k)
-
- fun empty cmpKey = OS{cmpKey = cmpKey, root = ref SplayNil, nobj = 0}
-
- fun singleton cmpKey v =
- OS{cmpKey= cmpKey,
- root = ref(SplayObj{value=v,left=SplayNil,right=SplayNil}),
- nobj=1}
-
- (* Primitive insertion. *)
- fun insert cmpKey (v,(nobj,root)) =
- case splay (cmpf cmpKey v, root) of
- (_,SplayNil) =>
- (1,SplayObj{value=v,left=SplayNil,right=SplayNil})
- | (EQUAL,SplayObj{value,left,right}) =>
- (nobj,SplayObj{value=v,left=left,right=right})
- | (LESS,SplayObj{value,left,right}) =>
- (nobj+1,
- SplayObj{
- value=v,
- left=SplayObj{value=value,left=left,right=SplayNil},
- right=right})
- | (GREATER,SplayObj{value,left,right}) =>
- (nobj+1,
- SplayObj{
- value=v,
- left=left,
- right=SplayObj{value=value,left=SplayNil,right=right}})
-
- (* Add an item. *)
- fun add (OS{cmpKey,root,nobj},v) = let
- val (cnt,t) = insert cmpKey (v,(nobj,!root))
- in
- OS{cmpKey=cmpKey, nobj=cnt, root=ref t}
- end
-
- (* Insert a list of items. *)
- fun addList (OS{cmpKey,root,nobj},l) = let
- val (cnt,t) = List.foldl (insert cmpKey) (nobj,!root) l
- in OS{cmpKey=cmpKey, nobj=cnt, root=ref t} end
-
- (* Look for an item, return NONE if the item doesn't exist *)
- fun peek (d as OS{cmpKey,root,nobj}, key) =
- case splay (cmpf cmpKey key, !root) of
- (_,SplayNil) => NONE
- | (EQUAL,r as SplayObj{value,...}) => (root := r; SOME value)
- | (_,r) => (root := r; NONE)
-
- (* Find an item *)
- fun member arg = ca rcnt+1)
- end
- val (root,cnt) = uni (!root) (!root')
- in
- OS{cmpKey = cmpKey, root = ref root, nobj = cnt}
- end
-
- (* Return a list of the items (and their keys) in the dictionary *)
- fun listItems (OS{root,...}) =
- let fun apply SplayNil res = res
- | apply (SplayObj{value,left,right}) res =
- apply left (value :: apply right res)
- in apply (!root) [] end
-
- (* Apply a function to the entries of the dictionary *)
- fun app af (OS{root,...}) =
- let fun apply SplayNil = ()
- | apply (SplayObj{value,left,right}) =
- (apply left; af value; apply right)
- in
- apply (!root)
- end
-
- fun revapp af (OS{root,...}) =
- let fun apply SplayNil = ()
- | apply (SplayObj{value,left,right}) =
- (apply right; af value; apply left)
- in apply (!root) end
-
- (* Fold function *)
- fun foldr abf b (OS{root,...}) =
- let fun apply SplayNil res = res
- | apply (SplayObj{value,left,right}) res =
- apply left (abf(value, apply right res))
- in apply (!root) b end
-
- fun foldl abf b (OS{root,...}) =
- let fun apply SplayNil res = res
- | apply (SplayObj{value,left,right}) res =
- apply right (abf(value, apply left b))
- in apply (!root) b end
-
- fun find p (OS{root,...}) =
- let fun ex SplayNil = NONE
- | ex (SplayObj{value=v,left=l,right=r}) =
- if p v then SOME v
- else case ex l of
- NONE => ex r
- | a => a
- in ex (!root) end
-